using Assets.Scripts.GDK;
using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;

namespace Brick
{
	public class SelectGuide : BaseGuideLayer
	{
		public List<RectTransform> itemList = new List<RectTransform>();

		public RectTransform LineBgBox;

		public RectTransform LineBox;

		public RectTransform ListBox;

		public RectTransform moveLineItem;

		public RectTransform LineBgBoxItem;

		public RectTransform LineBoxItem;

		public RectTransform pathItem;

		public override void OnStart()
		{
			for (int i = 0; i < itemList.Count; i++)
			{
				RectTransform rectTransform = itemList[i];
				rectTransform.gameObject.AddComponent<SelectGuideItem>();
			}
			LineBox.gameObject.AddComponent<SelectGuideBox>();
			bindItemTouch1();
		}

		private void bindItemTouch1()
		{
			List<string> keyList = new List<string>
		{
			"C",
			"A",
			"T"
		};
			initListBox(keyList);
			GameObject alert = gdk_game_manager.loadPrefab(GuideID.ALERT);
			alert.GetComponent<GuideAlert>().OnData(1);
			alert.transform.SetParent(box);
			List<int> list = new List<int>();
			list.Add(0);
			list.Add(1);
			list.Add(2);
			List<int> touchIndex = list;
			List<RectTransform> _itemList = getTouchItemIndexList(touchIndex);
			GDK.ModuleManger.Module<SelectGuideModule>().setTouchMoveNodeList(_itemList, delegate (bool isAll)
			{
				if (isAll)
				{
					UnityEngine.Object.Destroy(alert, 0f);
					gdk_game_manager.DispachEvent(EventID.OPEN_TRUE_ANSWER, keyList, obj2: false);
					bindItemTouch2();
				}
				touchEndFun();
			}, delegate (int len)
			{
				touchMoveFun(len, _itemList);
			}, delegate (Vector2 fromPos, Vector2 endPos)
			{
				touchMoveLineFun(fromPos, endPos);
			});
			initBoxItemList(_itemList);
		}

		private void bindItemTouch2()
		{
			List<string> keyList = new List<string>
		{
			"A",
			"C",
			"T"
		};
			initListBox(keyList);
			GameObject alert = gdk_game_manager.loadPrefab(GuideID.ALERT);
			alert.GetComponent<GuideAlert>().OnData(2);
			alert.transform.SetParent(box);
			List<int> list = new List<int>();
			list.Add(1);
			list.Add(0);
			list.Add(2);
			List<int> touchIndex = list;
			List<RectTransform> _itemList = getTouchItemIndexList(touchIndex);
			GDK.ModuleManger.Module<SelectGuideModule>().setTouchMoveNodeList(_itemList, delegate (bool isAll)
			{
				if (isAll)
				{
					UnityEngine.Object.Destroy(alert, 0f);
					gdk_game_manager.DispachEvent(EventID.OPEN_TRUE_ANSWER, keyList, obj2: false);
					UnityEngine.Object.Destroy(base.gameObject, 0.1f);
				}
				touchEndFun();
			}, delegate (int len)
			{
				touchMoveFun(len, _itemList);
			}, delegate (Vector2 fromPos, Vector2 endPos)
			{
				touchMoveLineFun(fromPos, endPos);
			});
			initBoxItemList(_itemList);
		}

		private void initListBox(List<string> keyList)
		{
		}

		private void touchEndFun()
		{
			moveLineItem.gameObject.SetActive(value: false);
			gdk_utils.removeAllChildren(LineBox);
		}

		private void touchMoveLineFun(Vector2 fromPos, Vector2 endPos)
		{
			moveLineItem.gameObject.SetActive(value: true);
			drawLineFun(moveLineItem, fromPos, endPos);
		}

		private void touchMoveFun(int len, List<RectTransform> _itemList)
		{
			List<RectTransform> list = new List<RectTransform>();
			for (int i = 0; i <= len; i++)
			{
				list.Add(_itemList[i]);
			}
			showSelectMoveLine(list);
		}

		private List<RectTransform> getTouchItemIndexList(List<int> touchIndex)
		{
			List<RectTransform> list = new List<RectTransform>();
			for (int i = 0; i < touchIndex.Count; i++)
			{
				RectTransform rectTransform = itemList[touchIndex[i]];
				if ((bool)rectTransform)
				{
					list.Add(rectTransform);
				}
			}
			return list;
		}

		private void initBoxItemList(List<RectTransform> _itemList)
		{
			showSelectLine(_itemList);
			showPathMove(_itemList);
		}

		private void showPathMove(List<RectTransform> moveNodeList)
		{
			List<Vector3> list = new List<Vector3>();
			foreach (RectTransform moveNode in moveNodeList)
			{
				Vector3 localPosition = moveNode.GetComponent<RectTransform>().localPosition;
				list.Add(localPosition);
			}
			pathItem.DOKill();
			pathItem.localPosition = list[0];
			Tweener t = pathItem.DOLocalPath(list.ToArray(), 2f);
			t.SetAutoKill(autoKillOnCompletion: false);
			t.SetLoops(-1);
			t.SetEase(Ease.Unset);
			t.Play();
		}

		private void showSelectMoveLine(List<RectTransform> moveNodeList)
		{
			gdk_utils.removeAllChildren(LineBox);
			RectTransform rectTransform = null;
			foreach (RectTransform moveNode in moveNodeList)
			{
				if ((bool)rectTransform)
				{
					Vector3 localPosition = rectTransform.GetComponent<RectTransform>().localPosition;
					Vector3 localPosition2 = moveNode.GetComponent<RectTransform>().localPosition;
					if (localPosition != localPosition2)
					{
						GameObject gameObject = Object.Instantiate(LineBoxItem.gameObject);
						gameObject.SetActive(value: true);
						RectTransform component = gameObject.GetComponent<RectTransform>();
						component.SetParent(LineBox, worldPositionStays: false);
						drawLineFun(component, localPosition, localPosition2);
					}
				}
				rectTransform = moveNode;
			}
		}

		private void showSelectLine(List<RectTransform> moveNodeList)
		{
			gdk_utils.removeAllChildren(LineBgBox);
			RectTransform rectTransform = null;
			foreach (RectTransform moveNode in moveNodeList)
			{
				if ((bool)rectTransform)
				{
					Vector3 localPosition = rectTransform.GetComponent<RectTransform>().localPosition;
					Vector3 localPosition2 = moveNode.GetComponent<RectTransform>().localPosition;
					if (localPosition != localPosition2)
					{
						GameObject gameObject = Object.Instantiate(LineBgBoxItem.gameObject);
						gameObject.SetActive(value: true);
						RectTransform component = gameObject.GetComponent<RectTransform>();
						component.SetParent(LineBgBox, worldPositionStays: false);
						drawLineFun(component, localPosition, localPosition2);
					}
				}
				rectTransform = moveNode;
			}
		}

		private void drawLineFun(RectTransform itemRT, Vector3 fromPos, Vector3 toPos)
		{
			float num = Vector3.Distance(fromPos, toPos);
			Vector3 localPosition = Vector3.Lerp(fromPos, toPos, 0.5f);
			float angle = Utils.TanAngle(fromPos, toPos);
			float x = (int)num;
			Vector2 size = itemRT.rect.size;
			itemRT.sizeDelta = new Vector2(x, size.y);
			itemRT.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
			itemRT.localPosition = localPosition;
		}
	}
}

